<?php
namespace Admin\Controller;
use Think\Controller;
class FloorController extends AdminController
{

    public function index()
    {

            $Proudct            = D('Proudct');

        //     $where              = '';
        //     $order              = '';
        //     $limit              = '';
        //     $num                = I('get.num') ? I('get.num') : 5;
        //     $order              = empty($order)?'max_id':$order;

        // //搜索设置
        //     if(I('get.keywords')){
        //         $where          = "proudctsname like '%".I('get.keywords')."%'";
        //     }else{
        //         $where          = '';
        //     }

            //设置分页
            //记录总数
            $count = $Proudct->where($where)->count();

            // var_dump($count);

             //创建分页对象
            // $page              = new \Think\Page($count,5);

            // $limit = $page->firstRow.','.$page->listRows;

            // $show = $page->show();



            // $proudctsArr       = $Proudct->where($where."state = 0")->order($order)->group('max_id')->limit($limit)->select();

              $proudctsArr       = $Proudct->where($where."state = 0")->order($order)->group('max_id')->select();
             
         

            // NSLog($flrArr);

            // NSLog($proudctsArr);

        //分配变量
            $this->assign('proudctsArr',$proudctsArr);

            $this->assign('page',$show);
    
            $this->display();
    }


      public function add()
      {
    


        $this->display();
      }



   public function insert()
   {


        $floor = D('Floor');

       if ($floor->create()) 
       {
           
                if ($floor->add()>0) 
                {
               
                $this->success('盖楼成功!',U('Admin/Floor/add'),2);

               }else{

                $this->error('盖楼失败!',U('Admin/Floor/add'),2);

                }
       }else{

           $this->error($floor->getError(),'',2);

            }
    
    }



    public function detail()
    {

       $floor = D('Floor');

       $data  = $floor->select();  //查询不在楼层的商品; 


       
       $this->assign('data',$data);
       $this->display();

    }




    public function insertDetail()
    {

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg','JPEG');// 设置附件上传类型
        $upload->rootPath = 'Public/Uploads/'; // 设置附件上传根目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES['img']);

        if(!$info) {// 上传错误提示错误信息
        $this->error($upload->getError());
        exit();
        }else{// 上传成功 获取上传文件信息
          $logo = $info['savepath'].$info['savename'];
        }
          $flr_detail = M('flr_detail');

          // var_dump($logo);

          // exit();

          $data = ['img'=>$logo,'floor_id'=>$_POST['floor_id'],'max_id'=>$_POST['max_id']];

          if ($flr_detail->add($data)>0) {


              $proudct            = M('product');

              $state              = ['state' => '1'];

              $proudct->where('max_id='.$_POST['max_id'])->save($state);
              
             $this->success('加入楼层成功!',U('Admin/Floor/index'),2);
          }else{

              $this->error('加入楼层失败!',U('Admin/Floor/FloorGoods'),2);

                }
     
      }




      public function FloorGoods()
    {

          
            $proudct            = M('product');

            $where              = '';
            $order              = '';
            $limit              = '';
            $num                = I('get.num') ? I('get.num') : 5;
            $order              = empty($order)?'max_id':$order;

        //搜索设置
            if(I('get.keywords')){
                $where          = "proudctsname like '%".I('get.keywords')."%'";
            }else{
                $where          = '';
            }


            $sql = "select * from  product,floor,flr_detail,sku_goods 
            where product.max_id = flr_detail.max_id 
            and flr_detail.floor_id = floor.id 
            and sku_goods.proudct_id = flr_detail.max_id 
            and product.state = 1"; 
            
            
            $proudctsArr       = $proudct->query($sql);


            $count             = count($proudctsArr);

            //设置分页
            //记录总数
            // var_dump($count);
            //创建分页对象
            
            $page              = new \Think\Page($count,5);

            $limit = $page->firstRow.','.$page->listRows;

            $show = $page->show();

            // $proudctsArr       = $proudct->where($where."state = 1")->order($order)->group('max_id')->limit($limit)->select();

            // $proudctsArr       = $proudct->alias('pro')

            // ->join('__FLR_DETAIL__ fd ON fd.max_id = pro.max_id ' )

            // ->join('LEFT JOIN __FLOOR__ fl ON fl.id=fd.id')

            // ->join('LEFT JOIN __SKU_GOODS__ sg ON fd.max_id = sg.proudct_id')

            // ->where($where."state = 1")

            // ->select();



/**
 打印调试区
 */
 // $data[$value['floor_name']] = $floor -> alias('fl')

            // $flr_detail        = M('flr_detail');

            // foreach ($proudctsArr as $key => $value) {
                  
            //      $flr_detailArr[] = $flr_detail->where('max_id='.$value['max_id'])->select();


            // }

 //        -> join('__FLR_DETAIL__ fd ON fd.max_id=fd.max_id') 

 //        -> join('__SKU_GOODS__ sg ON sg.proudct_id=fd.max_id') 

 //        -> join('__PRODUCT__ pro ON pro.id=fd.max_id')->where('fl.id='.$value['id'])

 //        ->select();



            // NSLog($proudctsArr);

               // NSLog($flr_detailArr);














        //分配变量
            $this->assign('proudctsArr',$proudctsArr);

            $this->assign('page',$show);
    
            $this->display();

  }



    public function MoveGoods(){

        if (empty($_GET['max_id'])) {
          
          $this->error('非法操作',2);
        }

          $flr = M('flr_detail');

          if ($flr->where('max_id='.$_GET['max_id'])->delete()) 
          {
              $proudct            = M('product');

              $state              = ['state' => '0'];

              $proudct->where('max_id='.$_GET['max_id'])->save($state);

            $this->success('移出楼层成功!',U('Admin/Floor/FloorGoods'),2);
          }else{

            $this->error('移出楼层失败!',U('Admin/Floor/FloorGoods'),2);

                }

          
  

    }


}